package com.hy.mydesktop.client.component.factory;

import com.extjs.gxt.ui.client.widget.Layout;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.hy.mydesktop.client.component.meta.AccordionPanelModelEnum;
import com.hy.mydesktop.client.component.meta.FormPanelModelEnum;
import com.hy.mydesktop.client.component.meta.TabItemModelEnum;
import com.hy.mydesktop.client.component.type.ComponentTypeEnum;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
import com.hy.mydesktop.shared.rpc.util.ComponentTypeEnumConverter;

/**
 * 
 * <ul>
 * <li>开发作者：冬超</li>
 * <li>设计日期：2010-8-30；时间：上午10:48:34</li>
 * <li>类型名称：TabItemFactory</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class TabItemFactory {
	public static TabItem createTabItem(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {

		return configureTabItem(gxtComponentMetaNodeModel);

	}

	/**
	 * 
	 * 
	 * @param gxtComponentMetaNodeModel
	 * @return
	 */
	private static TabItem configureTabItem(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {

		TabItem tabItem = createDefaultTabItem();
		tabItem = AbstractComponetFactory.configureAbstractComponet(tabItem, gxtComponentMetaNodeModel);

		if (gxtComponentMetaNodeModel.get(TabItemModelEnum.HIGHT.name()
				.toLowerCase()) != null) {
			Integer height = (Integer) gxtComponentMetaNodeModel
					.get(TabItemModelEnum.HIGHT.name().toLowerCase());
			tabItem.setHeight((int) height);

		} 
		if (gxtComponentMetaNodeModel.get(TabItemModelEnum.WIDTH.name()
				.toLowerCase()) != null) {
			Integer width = (Integer) gxtComponentMetaNodeModel
					.get(TabItemModelEnum.WIDTH.name().toLowerCase());
			tabItem.setWidth((int) width);

		} 
		if (gxtComponentMetaNodeModel.get(TabItemModelEnum.ADDTEXT.name()
				.toLowerCase()) != null) {
			String text = (String) gxtComponentMetaNodeModel
					.get(TabItemModelEnum.ADDTEXT.name().toLowerCase());
			tabItem.addText(text);

		} 
		if (gxtComponentMetaNodeModel.get(TabItemModelEnum.CLOSABLE.name()
				.toLowerCase()) != null) {
			Boolean closable = (Boolean) gxtComponentMetaNodeModel
					.get(TabItemModelEnum.CLOSABLE.name().toLowerCase());
			tabItem.setClosable(closable);

		} 
		if (gxtComponentMetaNodeModel.get(TabItemModelEnum.ENABLED.name()
				.toLowerCase()) != null) {
			Boolean enabled = (Boolean) gxtComponentMetaNodeModel
					.get(TabItemModelEnum.ENABLED.name().toLowerCase());
			tabItem.setEnabled(enabled);

		} 
		if (gxtComponentMetaNodeModel.get(TabItemModelEnum.ICON.name()
				.toLowerCase()) != null) {
			AbstractImagePrototype icon = (AbstractImagePrototype) gxtComponentMetaNodeModel
					.get(TabItemModelEnum.ICON.name().toLowerCase());
			tabItem.setIcon(icon);

		} 
		if (gxtComponentMetaNodeModel.get(TabItemModelEnum.TEXT.name()
				.toLowerCase()) != null) {
			String text = (String) gxtComponentMetaNodeModel
					.get(TabItemModelEnum.TEXT.name().toLowerCase());
			tabItem.setText(text);

		} 
		if (gxtComponentMetaNodeModel.get(TabItemModelEnum.STYLE.name()
				.toLowerCase()) != null) {
			String style = (String) gxtComponentMetaNodeModel
					.get(TabItemModelEnum.STYLE.name().toLowerCase());
			tabItem.setStyleName(style);

		}

		
		//设置布局
		if (gxtComponentMetaNodeModel.get(FormPanelModelEnum.LAYOUTINDEX.name()
				.toLowerCase()) != null) {

			Integer componentTypeEnumIndex = (Integer)gxtComponentMetaNodeModel.get(FormPanelModelEnum.LAYOUTINDEX.name().toLowerCase());
			ComponentTypeEnum componentTypeEnum = 	ComponentTypeEnumConverter.getComponentTypeEnum(componentTypeEnumIndex);

	
			Layout layout=null;
	
			switch (componentTypeEnum) {
	            case FITLAYOUT_MODEL:
	            		layout=FitLayoutFactory.createFitLayout(gxtComponentMetaNodeModel);
	            		tabItem.setLayout(layout);
	                break;
	            case ACCORDIONLAYOUT_MODEL:
	                	layout=AccordionLayoutFactory.createAccordionLayout(gxtComponentMetaNodeModel);
	                	tabItem.setLayout(layout);
	                break;
	            case ROWLAYOUT_MODEL:
	                	layout=RowLayoutFactory.createRowLayout(gxtComponentMetaNodeModel);
	                	tabItem.setLayout(layout);
	                break;
	            case VBOXLAYOUT_MODEL:
	                	layout=VboxLayoutFactory.createVboxLayout(gxtComponentMetaNodeModel);
	                	tabItem.setLayout(layout);
	                break;
	            case COLUMNLAYOUT_MODEL:
	                	layout=ColumnLayoutFactory.createColumnLayout(gxtComponentMetaNodeModel);
	                	tabItem.setLayout(layout);
	                break;
	            case FLOWLAYOUT_MODEL:
	                	layout=FlowLayoutFactory.createFlowLayout(gxtComponentMetaNodeModel);
	                	tabItem.setLayout(layout);
	                break;
	            case TABLELAYOUT_MODEL:
	                	layout=TableLayoutFactory.createTableLayout(gxtComponentMetaNodeModel);
	                	tabItem.setLayout(layout);
	                break;
	            case FORMLAYOUT_MODEL:
	                	layout=FormLayoutFactory.createFormLayout(gxtComponentMetaNodeModel);
	                	
	                	tabItem.setLayout(layout);
	                break;
	            default:
	                break;
	        }

		} 
		if (gxtComponentMetaNodeModel
				.get(AccordionPanelModelEnum.LAYOUT.name().toLowerCase()) != null) {
			ComponentTypeEnum componentTypeEnum = (ComponentTypeEnum) gxtComponentMetaNodeModel
					.get(AccordionPanelModelEnum.LAYOUT.name().toLowerCase());
			
			Layout layout=null;
			
				switch (componentTypeEnum) {
	                case FITLAYOUT_MODEL:
	                		layout=FitLayoutFactory.createFitLayout(gxtComponentMetaNodeModel);
	                		tabItem.setLayout(layout);
		                break;
	                case ACCORDIONLAYOUT_MODEL:
		                	layout=AccordionLayoutFactory.createAccordionLayout(gxtComponentMetaNodeModel);
		            		tabItem.setLayout(layout);
		                break;
	                case ROWLAYOUT_MODEL:
		                	layout=RowLayoutFactory.createRowLayout(gxtComponentMetaNodeModel);
	                		tabItem.setLayout(layout);
		                break;
	                case VBOXLAYOUT_MODEL:
		                	layout=VboxLayoutFactory.createVboxLayout(gxtComponentMetaNodeModel);
	                		tabItem.setLayout(layout);
		                break;
	                case COLUMNLAYOUT_MODEL:
		                	layout=ColumnLayoutFactory.createColumnLayout(gxtComponentMetaNodeModel);
	                		tabItem.setLayout(layout);
		                break;
	                case FLOWLAYOUT_MODEL:
		                	layout=FlowLayoutFactory.createFlowLayout(gxtComponentMetaNodeModel);
	                		tabItem.setLayout(layout);
		                break;
	                case TABLELAYOUT_MODEL:
		                	layout=TableLayoutFactory.createTableLayout(gxtComponentMetaNodeModel);
	                		tabItem.setLayout(layout);
		                break;
	                case FORMLAYOUT_MODEL:
		                	layout=FormLayoutFactory.createFormLayout(gxtComponentMetaNodeModel);
		                	
	                		tabItem.setLayout(layout);
		                break;
	                default:
		                break;
                }
		}
		return tabItem;
	}

	private static TabItem createDefaultTabItem() {

		TabItem tabItem = new TabItem();
		return tabItem;
	}
}
